package com.gordon.wiki.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gordon.wiki.pojo.Content;
import com.gordon.wiki.pojo.Doc;
import com.gordon.wiki.req.DocQueryReq;
import com.gordon.wiki.req.DocSaveReq;
import com.gordon.wiki.resp.DocQueryResp;
import com.gordon.wiki.resp.PageResp;
import com.gordon.wiki.result.Result;
import com.gordon.wiki.result.ResultUtil;
import com.gordon.wiki.service.ContentService;
import com.gordon.wiki.service.DocService;
import com.gordon.wiki.service.EbookService;
import com.gordon.wiki.utils.CopyUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;

/**
 * @Projectname: wiki
 * @Filename: DocController
 * @Author: GordonLo
 * @Data:2022/11/16 9:38
 * @Description: TODO
 */
@RestController
@RequestMapping("/doc")
public class DocController {

    private static final Logger LOG = LoggerFactory.getLogger(EbookService.class);

    @Autowired
    private DocService docService;

    @Autowired
    private ContentService contentService;

    @GetMapping("/findContentById/{id}")
    public Result findContentById(@PathVariable int id) {
        Content content = contentService.getById(id);
        docService.increaseViewCount(id);

//        CommonResp<String> resp = new CommonResp<>();
        if (content != null && content.getContent() != null) {
            return ResultUtil.success(content.getContent());
        } else {
            return ResultUtil.failure();
        }

    }

    @GetMapping("/getDocListByPage")
    public Result getDocListByPage(DocQueryReq req) {
        Page<Doc> page = new Page<>(req.getPage(), req.getSize());
        page = docService.page(page);
        List<Doc> list = page.getRecords();

        LOG.info("总行数：{}", page.getTotal() + "");
        LOG.info("总页数：{}", page.getPages() + "");

        List<DocQueryResp> docResps = CopyUtil.copyList(list, DocQueryResp.class);

        PageResp<DocQueryResp> respPage = new PageResp<>();
        respPage.setTotal(page.getTotal());
        respPage.setList(docResps);

//        CommonResp<PageResp<DocQueryResp>> resp = new CommonResp<>();
//        resp.setContent(respPage);

        return ResultUtil.success(respPage);
    }


    @GetMapping("/allList/{ebookId}")
    public Result allList(@PathVariable Long ebookId) {
        QueryWrapper<Doc> wrapper = new QueryWrapper<>();
        wrapper.eq("ebook_id", ebookId);
        List<Doc> list = docService.list(wrapper);

        List<DocQueryResp> resp = CopyUtil.copyList(list, DocQueryResp.class);

//        CommonResp<List<DocQueryResp>> listCommonResp = new CommonResp<>();
//        listCommonResp.setContent(resp);
        return ResultUtil.success(resp);
    }

    @PostMapping("/save")
    public Result save(@Valid @RequestBody DocSaveReq req) {
        Doc doc = CopyUtil.copy(req, Doc.class);
        docService.saveOrUpdate(doc);

        Content content = CopyUtil.copy(req, Content.class);
        contentService.saveOrUpdate(content);

//        CommonResp resp = new CommonResp<>();
        return ResultUtil.success();
    }

//    @GetMapping("/remove")
//    public Result remove(int id) {
//
//        docService.removeById(id);
//
////        CommonResp<Object> resp = new CommonResp<>();
//        return ResultUtil.success();
//    }

    @DeleteMapping("/remove")
    public Result remove(Integer[] ids) {
        List<Integer> list = Arrays.asList(ids);
        docService.removeByIds(list);

//        CommonResp<Object> resp = new CommonResp<>();
        return ResultUtil.success();
    }

    @GetMapping("/vote/{id}")
    public Result vote(@PathVariable int id) {
        docService.increaseVoteCount(id);

//        CommonResp<Object> resp = new CommonResp<>();
        return ResultUtil.success();
    }

}